package com.springbootstudy.demo.service;


import com.springbootstudy.demo.dao.UserDao;
import com.springbootstudy.demo.entity.User;
import com.springbootstudy.demo.utils.IdWorker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;

@Service
public class UserServiceImpl implements UserService {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	// 注入Service依赖
	@Autowired
	public UserDao userDao;

	/**
	 * 添加用户
	 */
	@Transactional
	@Override
	public String addUser(String user_account, String user_password,
						  String user_name) {
		final IdWorker w = new IdWorker(1, 2);
		long l = w.nextId();
		String user_uid = "u" + l;
		String user_key = "key" + l;
		userDao.insertUser(user_uid, user_account, user_password, user_name,
				user_key);
		return "1";
	}

	/**
	 * 根据用户名获得用户信息
	 */
	@Override
	public User getUserInfo(String user_account) {
		System.out.println("zzz1:"+userDao);
		User userInfo = userDao.getUser(user_account);
		System.out.println("zzz2"+userInfo);
		return userInfo;
	}

	/**
	 * 获得账号的数量
	 */
	@Override
	public Integer getUserNumer(String user_account) {
		System.out.println("zzz3:"+userDao);
		Integer querynumber = userDao.getUserNumer(user_account);
		System.out.println("zzz4:"+querynumber);
		return querynumber;
	}

	/**
	 * 查询全部用户
	 */
	@Override
	public ArrayList<User> QueryALL() {
		ArrayList<User> userInfos = userDao.QueryALL();
		return userInfos;
	}

	/**
	 * 根据ID删除用户
	 */
	@Override
	public void deleteUser(String user_id) {
		userDao.deleteUser(user_id);

	}


}
